package com.qjj.studydainty.Db;
/*
 * Title:       <br>
 * Description: <br>
 *
 * @author 曲建金
 * @version 1.0
 */

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qjj.studydainty.entity.History;
import com.qjj.studydainty.entity.Marker;

import java.util.ArrayList;
import java.util.List;

public class DbUtil {

    public static void insertIntoHistory(Context context, History history) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("INSERT INTO history VALUES (NULL, ?, ?)",
                new Object[]{history.getName(), history.getUrl()});
        db.close();
    }

    public static void deleteByIdFromHistory(Context context, int id) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("Delete from history where id=?", new Object[]{id});
        db.close();
    }

    public static void deleteAllFromHistory(Context context) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("Delete from history", new Object[]{});
        db.close();
    }

    public static List<History> queryFromHistory(Context context) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();

        List<History> historyList = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM history", new String[]{});
        while (cursor.moveToNext()) { //遍历结果集
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String url = cursor.getString(cursor.getColumnIndex("url"));

            History history = new History(id, name, url);
            historyList.add(history);
        }
        cursor.close(); //关闭cursor
        db.close(); //关闭数据库连接
        return historyList;
    }



    public static void insertIntoMarker(Context context, Marker history) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("INSERT INTO marker VALUES (NULL, ?, ?)",
                new Object[]{history.getName(), history.getUrl()});
        db.close();
    }

    public static void deleteByIdFromMarker(Context context, int id) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("Delete from marker where id=?", new Object[]{id});
        db.close();
    }

    public static void deleteAllFromMarker(Context context) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("Delete from marker", new Object[]{});
        db.close();
    }

    public static List<Marker> queryFromMarker(Context context) {
        DBHelper helper = new DBHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();

        List<Marker> markerList = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM marker", new String[]{});
        while (cursor.moveToNext()) { //遍历结果集
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String url = cursor.getString(cursor.getColumnIndex("url"));

            Marker marker = new Marker(id, name, url);
            markerList.add(marker);
        }
        cursor.close(); //关闭cursor
        db.close(); //关闭数据库连接
        return markerList;
    }

}
